<template>
  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">
  </forms>
</template>

<script>
import s94Mjs from "s94-js";
import {field_type, iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
import {decode_config} from "@/js/s94_tool.js";

export default {
  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let model_name = this.$get('model_name');

    let method_map = {
      'eq':'等于(=)', 'neq':'不等于(<>)',
      'gt':'大于(>)', 'egt':'大于或等于(>=)',
      'lt':'小于(<)', 'elt':'小于或等于(<=)',
      'like':'包含(like)', 'notlike':'不包含(not like)',
      'in':'在列表中(in)', 'notin':'不在列表中(not in)',
      'between':'介于之间(between)', 'notbetween':'不介于之间(not between)',
    }
    let type_map = field_type(field_type.CAN_MODEL_FIELD, 'label');

    let forms_config = {
      model_name: {label:'模型',name:'model_name',type:'select_one',config:[],tips:'',value: model_name||''},
      field: {label:'字段名',name:'field',type:'text_one',config:[],tips:'选择检索的数据库字段',value:''},
      method: {label:'检索方式',name:'method',type:'select_one',tips:'',config:method_map,value:'eq'},
      label: {label:'别名',name:'label',type:'text_one',tips:''},
      tips: {label:'介绍',name:'tips',type:'text_one',tips:'参数简介、提示'},

      type: {label:'输入类型',name:'type',type:'select_one',config:type_map,tips:'选择参数的组件类型',value:''},
      config: {label:'组件配置',name:'config',type:'text_more',tips:''},
      form_props: {label:'扩展配置',name:'form_props',type:'text_one',tips:'json字符',value:'{}'},

      value: {label:'组件默认值',name:'value',type:'text_one',tips:'默认值'},

      sort: {label:'排序',name:'sort',type:'text_one',config:'number',tips:'排序索引，大>小', value:100},
    }
    if (id){
      forms_config.model_name.is_info = true;
      forms_config.create_time = {label:'创建时间',name:'create_time',tips:''};
    }
    if (this.$get('is_info')){
      s94Mjs.each(forms_config, function (row){ row.is_info = true; });
      btns = [];
    }
    return {
      id: id,
      model_name: model_name,
      btns: btns,
      config: forms_config,
      is_default: 0,
      name_has_point: false,
    }
  },
  methods: {
    param(data){
      data = data || {};
      data.model_name = this.model_name;
      return s94Mjs.param_encode(data);
    },
    onsubmit(formData){
      let url = this.id ? `/admin/model_v2_where_param/edit?id=`+this.id : '/admin/model_v2_where_param/add';
      ajax({url:url, method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('model_v2_where_param/index', '$refs.list_url.list');
          iframe.hide('model_v2_where_param/edit');
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index');
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
  },
  watch: {
    'config.type.value'(v){
      this.config.value.type = v=="text_more" ? "text_more" : "text_one";
      s94Mjs.each(field_type.component_config(v), (v,k)=>{
        this.config.config[k] = v;
      })
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (this.id) {
      ajax({url:'/admin/model_v2_where_param/info?id='+this.id, cache:true}, (res)=>{
        this.$refs['forms'].init(res.data);
        return false;
      }, ()=>{
        if (this.__iframe){
          iframe.remove('model_v2_where_param/edit');
        }else {
          this.$router.push('index');
        }
      })
    }else {
      this.config.type.value = 'text_one';
    }

    //其他初始化操作
    ajax({url:'/admin/index/model_v2_list',cache:true},(res)=>{
      this.config.model_name.config = decode_config(res.data.list, {content_key:'label', value_key:'name'});
      return false
    })
  }
}
</script>
